package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.view.activity.AgainBackupsActivity;

import java.util.List;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/7/10.
 */

public class AgainBackGrid2Adapter extends BaseAdapter {
    private Context context;
    private List<String> datas;
    private OnClick click;

    public AgainBackGrid2Adapter(AgainBackupsActivity againBackupsActivity, List<String> datas) {
        this.context = againBackupsActivity;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.backups_grid_item, null);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.content = (TextView) view.findViewById(R.id.content);
        holder.linear = (LinearLayout) view.findViewById(R.id.linear);
        holder.content.setText(datas.get(i));
        final ViewHolder finalHolder = holder;
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (finalHolder.content.isSelected()) {
                    finalHolder.content.setBackgroundColor(context.getResources().getColor(R.color.colorToolbarBack));
                    finalHolder.content.setSelected(false);
                    click.click(i,true);
                } else {
                    finalHolder.content.setBackground(context.getResources().getDrawable(R.drawable.agaibackups_grid2_bg));
                    finalHolder.content.setSelected(true);
                    click.click(i,false);
                }
            }
        });
        return view;
    }

    public class ViewHolder {
        private TextView content;
        private LinearLayout linear;
    }

    public interface OnClick {
        void click(int psw,boolean isSelect);
    }

    public void getPst(OnClick click) {
        this.click = click;
    }
}
